#include <OneWire.h>

/* DS18S20 Temperature chip i/o

 */

OneWire  ds(10);  // on pin 10
byte tempSensorAddress[8];
byte portAddress[8];

void setup(void) {
  // initialize inputs/outputs
  // start serial port
  Serial.begin(9600);
  findDevices();
}



void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  readTemp();
  delay(1000);
}

void findDevices(){
  boolean done = false;
  byte i;
  byte addr[8];

  while(!done) {
  if ( !ds.search(addr)) {
//     if(tempSensorAddress != 0) {Serial.print("Temprature Sensor at "); Serial.println(tempSensorAddress, HEX); }
//     if(portAddress != 0) {Serial.print("I/O port at "); Serial.println(portAddress, HEX); }
	Serial.println("No more devices found");
	ds.reset_search();
	  done = true;
  }
   else
   {
     // display address
	 Serial.print("Address = ");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }
  Serial.println(" ");
    if ( OneWire::crc8( addr, 7) != addr[7]) {
	Serial.print("CRC is not valid!\n");
    }
	else {
	  if ( addr[0] == 0x3B) {
	Serial.print("Device is DS1825\n");
	for( i = 0; i < 8; i++) {
	 tempSensorAddress[i] =  addr[i];
	}
     }
   if ( addr[0] == 0x3A) {
	Serial.print("Device is DS1413\n");
	for( i = 0; i < 8; i++) {
	 portAddress[i] =  addr[i];
	}
     }
	   }  // end of else for CRC not valid
  }	   // end of else for got no more addresses
   }	  // end of while not done
}

void readTemp(){
  byte data[12];
   ds.reset();
  ds.select(tempSensorAddress);
  ds.write(0x44);	   // Convert temp

  delay(1000);
  ds.reset();
  ds.select(tempSensorAddress);
  ds.write(0xBE);	   // Read Scratchpad

  Serial.print(" ");
  for ( int i = 0; i < 9; i++) {	     // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print( OneWire::crc8( data, 8), HEX);
  Serial.println();
}

 

